package jinyilw.swing.rlaf.ui;

import jinyilw.swing.FontTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicMenuUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

/**
 * @author Jinyi
 */
public class RMenuUI extends BasicMenuUI
{
	JMenu menu;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public static ComponentUI createUI(JComponent c)
	{
		return new RMenuUI();
	}

	@Override
	public void installUI(JComponent c)
	{
		super.installUI(c);
		menu = (JMenu) c;
		menu.setOpaque(false);
		menu.setBorderPainted(false);
		menu.setFont(FontTools.defaultFont);
		selectionBackground = RThemeColor.darkThemeColor;
		selectionForeground = Color.white;
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setTextAADrawing(g2d);
		paint(g, c);
		renderingConfig.resetTextAADrawing(g2d);
	}

	@Override
	protected void paintText(Graphics g, JMenuItem menuItem, Rectangle textRect,
			String text)
	{
		RMenuItemUI.paintMenuText(g, menuItem, textRect, text,
				selectionForeground);
	}

	@Override
	protected void paintBackground(Graphics g, JMenuItem menuItem,
			Color bgColor)
	{
		RMenuItemUI.paintMenuBackground(g, menuItem, bgColor);
	}
}
